<?php
/**
 * 用户现金帐户\积分服务类.
 * 
 */
class UserAccountService extends Service {
	
	/**
	 * 将通过登陆用户id查询对应账户信息。若不存在则新增对应账户
	 */
	public function get4User(){
		$uid = $_SESSION['mid'];
		if(empty($uid))return null;
		$where['uid']=$uid;
		$uaccountdao = M('Uaccount');
		$uaccout = $uaccountdao->where($where)->find();
		if(empty($uaccout)){
  			$data['uid']=$uid;
 	 		$data['ctime']=time();
			$uaccountdao->add($data);
			unset($uaccout);
			$uaccout = $this->get4User();
		}
		return $uaccout;
	}
	
	/**
	 * 通过现金帐户ID查找。
	 * @param unknown_type $uaccount_id
	 */
	public function get4UserById($uaccount_id){
		$where['id']=$uaccount_id;
		$uaccountdao = M('Uaccount');
		$uaccout = $uaccountdao->where($where)->find();
		return $uaccout;
	}
	
	/**
	 * 查询当前登陆用户的积分分页数据。
	 * @param $atype		查看的积分类型	'in'--收入   'out'--支出
	 */
	public function search4UserAccountCash($atype='in',$limit=15){

		if($atype=='out'){
			$uacount = $this->get4User();
			$pagedata = service('Pay')->searchItemPay($_SESSION['mid'],null,null,'xxypay',$limit,'Y','all');
		}
		if($atype=='in'){
			$uacount = $this->get4User();
			$pagedata = service('Pay')->searchItemPay(null,$uacount['id'],'pay_charge',null,$limit,'Y','all');
		}
		return $pagedata;
	}
	
	
	/**
	 * 登陆用户更新现金帐户。
	 * 所有数据重新计算。
	 */
	public function refresh4UserCash(){
		Log::record('<<<<<现金帐户-刷新，用户ID['.$_SESSION['mid'].']：开始--'.date('Y-m-d H:i:s'),Log::K_ORDER,Log::FILE);
		
		$uaccount = $this->get4User();
		$totalamount = service('Pay')->countItemPay($_SESSION['mid'],null,null,'xxypay','all');
		Log::record('现金帐户-支付总金额['.$totalamount.']',Log::K_ORDER,Log::FILE);
		
		$chargeamount = service('Pay')->countItemPay(null,$uaccount['id'],'pay_charge',null,'all');
		Log::record('现金帐户-充值总金额['.$chargeamount.']',Log::K_ORDER,Log::FILE);
		
		$uaccountdata['id'] = $uaccount['id'];
		$uaccountdata['current_money'] = $chargeamount-$totalamount;
		$uaccountdata['used_money'] = $totalamount;
		M('Uaccount')->save($uaccountdata);
		unset($uaccout);
		$uaccout = $this->get4User();
		Log::record('>>>>>现金帐户-刷新结果:->['.parseAry2Str($uaccout).']',Log::K_ORDER,Log::FILE);
		Log::save(); 
		return $uaccout;
	}
	
	
	/**
	 * 查询当前登陆用户的积分分页数据。
	 * @param $stype		查看的积分类型	'gain'--获得的积分  'used'--已使用的积分
	 */
	public function search4UserCredit($stype='gain',$limit=15){
		if($stype!='gain'&&$stype!='used')return null;
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		if($stype=='gain')$where['gain_credit']=array('gt',0);
		if($stype=='used')$where['used_credit']=array('gt',0);
		$order='ctime desc';
		$orderdao = M('Order');
		$pagedata = $orderdao->kPageQuery($where,$order,$limit);
		if(!empty($pagedata)&&!empty($pagedata['data'])){
			if($stype=='gain') {
				foreach ($pagedata['data'] as $vo){
					$creditdata[]=array(	'mode'=>'gain'//记录积分获得方式
											,'credit'=>$vo['gain_credit']//对应积分
											,'gtime'=>$vo['pay_time']//对应积分
										);
				}
			}
			if($stype=='used') {
				foreach ($pagedata['data'] as $vo){
					$creditdata[]=array(	'mode'=>'used'//记录积分获得方式
											,'credit'=>$vo['used_credit']//对应积分
											,'gtime'=>$vo['pay_time']//对应积分
										);
				}
			}
			$pagedata['data']=$creditdata;
		}
		return $pagedata;
	}
	
	
	/**
	 * 查询当前登陆使用的积分。
	 */
	public function count4UserCreditTotalUsed(){
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		$where['used_credit']=array('gt',0);
		$orderdao = M('Order');
		$result =	$orderdao->where($where)->limit(1)->field('sum(`used_credit`) as totalcredit')->find();
		$totalcredit	=	$result['totalcredit'];
		return $totalcredit;
	}
	
	
	/**
	 * 查询当前登陆使用的积分。
	 */
	public function count4UserCreditTotalGain(){
		$where['user_id']=$_SESSION['mid'];
		$where['pay_state']='pay';
		$where['order_state']='N';
		$where['enable']='Y';
		$where['gain_credit']=array('gt',0);
		$orderdao = M('Order');
		$result =	$orderdao->where($where)->limit(1)->field('sum(`gain_credit`) as totalcredit')->find();
		$totalcredit	=	$result['totalcredit'];
		return $totalcredit;
	}
	
	
	//服务初始化
	public function init() {
	}

	//运行服务，系统服务自动运行
	public function run() {
	}
	
	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>